<!-- 书签组件 -->
<template>
  <div class="ebook-bookmark" ref="bookmark">
    <div class="ebook-bookmark-text-wrapper">
      <div class="ebook-bookmark-down-wrapper" ref="iconDown">
        <span class="icon-down"></span>
      </div>
      <div class="ebook-bookmark-text">{{ text }}</div>
    </div>
    <div class="ebook-bookmark-icon-wrapper" :style="isFixed ? fixedStyle : {}">
      <bookmark :color="color" :width="15" :height="35"></bookmark>
    </div>
  </div>
</template>

<script>
import { realPx } from '../../utils/utils'
import Bookmark from '../common/Bookmark'
import { ebookMixin } from '../../utils/mixin'
import { getBookmark, saveBookmark } from '../../utils/localStorage'
const BLUE = '#346cbc'
const WHITE = '#fff'
// 书签组件
export default {
  mixins: [ebookMixin],
  components: {
    Bookmark
  },
  data() {
    return {
      text: '',
      color: WHITE,
      isFixed: false
    }
  },
  computed: {
    height() {
      return realPx(35)
    },
    // 临界值 自己可设
    threshold() {
      return realPx(50)
    },
    fixedStyle() {
      // clientWidth 是组件宽度
      return {
        position: 'fixed',
        top: 0
        // right: `${(window.innerWidth - this.$refs.bookmark.clinetWidth) / 2}px`
      }
    }
  },
  methods: {
    beforeHeight(V) {
      // 状态1：未超过书签的高度
      if (this.isBookmark) {
        this.text = this.$t('book.pulldownDeleteMark')
        this.color = BLUE
        this.isFixed = true
      } else {
        this.text = this.$t('book.pulldownAddMark')
        this.color = WHITE
        this.isFixed = false
      }
    },
    beforeThreshold(v) {
      // 状态2：未到达临界状态
      // console.log('到达第二阶段')
      // 吸顶效果.
      this.$refs.bookmark.style.top = `${-v}px`
      // 判断当前页是否为书签页
      if (this.isBookmark) {
        this.text = this.$t('book.pulldownDeleteMark')
        this.color = BLUE
      } else {
        this.text = this.$t('book.pulldownAddMark')
        this.color = WHITE
      }
      const iconDown = this.$refs.iconDown
      if (iconDown.style.transform === 'rotate(180deg)') {
        // 箭头旋转180度
        iconDown.style.transform = 'rotate(0deg)'
      }
    },
    afterThreshold(v) {
      // 状态3：超越临界状态
      // console.log('到达第三阶段')
      this.$refs.bookmark.style.top = `${-v}px`
      if (this.isBookmark) {
        this.text = this.$t('book.releaseDeleteMark')
        this.color = WHITE
        this.isFixed = false
      } else {
        this.text = this.$t('book.releaseAddMark')
        this.color = BLUE
        this.isFixed = true
      }
      const iconDown = this.$refs.iconDown
      if (iconDown.style.transform === '' || iconDown.style.transform === 'rotate(0deg)') {
        // 箭头旋转180度
        iconDown.style.transform = 'rotate(180deg)'
      }
    },
    // 松手之后 v === 0归位
    restore() {
      // 状态4：归位
      // 动画结束之后归位，所以用到延迟
      setTimeout(() => {
        this.$refs.bookmark.style.top = `${-this.height}px`
        this.$refs.iconDown.style.transform = 'rotate(0deg)'
      }, 200)
      if (this.isFixed) {
        this.setIsBookmark(true)
        this.addBookmark()
      } else {
        this.setIsBookmark(false)
        this.removeBookmark()
      }
    },
    // 存储书签
    addBookmark() {
      // 存储书签在localStorange
      this.bookmark = getBookmark(this.fileName)
      if (!this.bookmark) {
        this.bookmark = []
      }
      const currentLocation = this.currentBook.rendition.currentLocation()
      // console.log(currentLocation)
      // 找到感叹号之后的内容变成空，返回前面的
      if (currentLocation) {
        const cfibase = currentLocation.start.cfi.replace(/!.*/, '')
        const cfistart = currentLocation.start.cfi.replace(/.*!/, '').replace(/\)$/, '')
        const cfiend = currentLocation.end.cfi.replace(/.*!/, '').replace(/\)$/, '')
        // console.log(cfibase, cfistart, cfiend)
        const cfirange = `${cfibase}!,${cfistart},${cfiend})`
        this.currentBook.getRange(cfirange).then(range => {
          // console.log(range.toString())
          // 去掉两个空格全局
          const text = range.toString().replace(/\s\s/g, '')
          // console.log(text)
          this.bookmark.push({
            cfi: currentLocation.start.cfi,
            text: text
          })
          // 保存书签页
          saveBookmark(this.fileName, this.bookmark)
        })
      }
    },
    //  删除书签
    removeBookmark() {
      let currentLocation
      if (this.currentBook.rendition) {
        currentLocation = this.currentBook.rendition.currentLocation()
      }
      if (currentLocation) {
        const cfi = currentLocation.start.cfi
        this.bookmark = getBookmark(this.fileName)
        // console.log(this.bookmark)
        if (this.bookmark) {
          this.bookmark = this.bookmark.filter(item => item.cfi !== cfi)
          saveBookmark(this.fileName, this.bookmark)
          // console.log(this.bookmark)
          // 当前页不是书签
          this.setIsBookmark(false)
        }
      }
    }
  },
  watch: {
    // 从vuex中监听y偏移量，所以要mixin，中的getter监听
    offsetY(v) {
      // 以下情况不监听
      if (!this.bookAvailable || this.menuVisible || this.settingVisible >= 0) {
        return
      }
      if (v >= this.height && v <= this.threshold) {
        this.beforeThreshold(v)
      } else if (v > this.threshold) {
        this.afterThreshold(v)
      } else if (v > 0 && v < this.height) {
        this.beforeHeight(v)
      } else if (v === 0) {
        this.restore()
      }
    },
    // 刷新之后监听当前页是否为书签
    isBookmark(isBookmark) {
      this.isFixed = isBookmark
      if (isBookmark) {
        this.color = BLUE
        // this.isFixed = true
      } else {
        this.color = WHITE
        // this.isFixed = false
      }
    }
  }
}
</script>

<style lang='scss' scoped>
@import "../../assets/styles/global";

.ebook-bookmark {
  position: absolute;
  top: px2rem(-35);
  left: 0;
  z-index: 200;
  width: 100%;
  height: px2rem(35);
  // background-color:pink;
  .ebook-bookmark-text-wrapper {
    position: absolute;
    right: px2rem(45);
    bottom: 0;
    display: flex;
    .ebook-bookmark-down-wrapper {
      font-size: px2rem(14);
      color: white;
      transition: all 0.2s linear;
    }
    .ebook-bookmark-text {
      font-size: px2rem(14);
      color: white;
    }
  }
  .ebook-bookmark-icon-wrapper {
    position: absolute;
    right: 0;
    bottom: 0;
    margin-right: px2rem(15);
  }
}
</style>
